package pl.uz.zgora.plan;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class NauczycielViewActivity extends Activity {

	private static ProgressDialog dialog = null;
	private NauczycielInfo nauczycielInfo = null;
	
	@Override
	public Object onRetainNonConfigurationInstance() {
	    return nauczycielInfo;
	}
	
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.teacher_view);
	    
	    Bundle intentParams = getIntent().getExtras();
	    final int id = intentParams.getInt("id");
		final String nazwa = intentParams.getString("nazwa");
		
		if(HelperMethodsManager.isOnline(this)) {
			dialog = HelperMethodsManager.getProgressDialog(this);
			dialog.show();
			
			final NauczycielInfo lastNonConfigurationInstance = (NauczycielInfo) getLastNonConfigurationInstance();
			if(lastNonConfigurationInstance == null) {
				Thread handleActivity = new Thread(new Runnable() {

					public void run() {
						final SOAPManager soapManager = new SOAPManager(NauczycielViewActivity.this);

						try {
							nauczycielInfo = soapManager.downloadTeacherInfoFromPers(id);
							manageTeacherInfoUI(nauczycielInfo, id, nazwa);
							dialog.dismiss();
						} catch (IOException e) {
							dialog.dismiss();
							HelperMethodsManager.manageException("NauczycielViewActivity::onCreate()", "IOException",  e.getLocalizedMessage(), NauczycielViewActivity.this);
						} catch (XmlPullParserException e) {
							dialog.dismiss();
							HelperMethodsManager.manageException("NauczycielViewActivity::onCreate()", "XmlPullParserException",  e.getLocalizedMessage(), NauczycielViewActivity.this);
						} catch(Exception e) {
							dialog.dismiss();
							HelperMethodsManager.manageException("NauczycielViewActivity::onCreate()", "Exception",  e.getLocalizedMessage(), NauczycielViewActivity.this);
						}
					}
				});

				handleActivity.start();
			} else {
				nauczycielInfo = lastNonConfigurationInstance;
				manageTeacherInfoUI(nauczycielInfo, id, nazwa);
				dialog.dismiss();
			}
		} else {
			Dialog errorDialog = HelperMethodsManager.getConnectionErrorDialog(this, this);
	    	errorDialog.show();
		}
	}
	
	private void manageTeacherInfoUI(final NauczycielInfo teachersInfo, final Integer teacherID, final String teacherName) {
		if(teachersInfo == null) { throw new IllegalArgumentException(); }
		final TextView tv_email = (TextView) findViewById(R.id.teacher_info_email);
		final TextView tv_building = (TextView) findViewById(R.id.teacher_info_building);
		final TextView tv_adres = (TextView) findViewById(R.id.teacher_info_adres);
		final TextView tv_name = (TextView) findViewById(R.id.teacher_info_name);
		final TextView tv_room = (TextView) findViewById(R.id.teacher_info_room);
		final ImageView foto = (ImageView) findViewById(R.id.teacher_info_foto);
		final LinearLayout infoLayout = (LinearLayout) findViewById(R.id.teacher_info_panel);
		final TextView tv_error = (TextView) findViewById(R.id.tv_app_error);
		infoLayout.post(new Runnable() {

			public void run() {
				tv_name.setText(teacherName);
				if(!teachersInfo.getPokoj().matches(".*[0-9a-zA-Z]+.*")) {
					tv_error.setVisibility(View.VISIBLE);
					tv_error.setText("Brak danych o wykładowcy w systemie PERS");
					infoLayout.setVisibility(View.GONE);
				} else {
					tv_room.setText(Html.fromHtml("<b>Pokój</b>: " + teachersInfo.getPokoj()));
					tv_email.setText(Html.fromHtml("<b>e-mail</b>: " + teachersInfo.getEmail()));
					tv_building.setText(Html.fromHtml("<b>Budynek</b>: " + teachersInfo.getBudynek()));
					tv_adres.setText(Html.fromHtml("<b>Adres</b>: " + teachersInfo.getAdres()));
					infoLayout.setVisibility(View.VISIBLE);
					try {
						final URL fotoURL = new URL(teachersInfo
								.getFoto() + String.valueOf(teacherID));
						Bitmap bitmap = BitmapFactory
								.decodeStream((InputStream)fotoURL.getContent());
						if(bitmap != null) {
							foto.setImageBitmap(bitmap);
						}
					} catch (MalformedURLException e) {
						HelperMethodsManager.manageException("NauczycielViewActivity::manageTeacherInfoUI()", "MalformedURLException", e.getLocalizedMessage(), NauczycielViewActivity.this);
					} catch (IOException e) {
						HelperMethodsManager.manageException("NauczycielViewActivity::manageTeacherInfoUI()", "IOException", e.getLocalizedMessage(), NauczycielViewActivity.this);
					}
				}
			}
		});
	}
}
